import 'package:flutter/material.dart';
class Test11 extends StatefulWidget{
    _TestState createState() => _TestState();
}

class _TestState extends State<Test11> {

    @override
    Widget build(BuildContext context) {

        return new Scaffold(
            appBar: AppBar(
                title: Text("可展开列表"),
                centerTitle: true,
            ),
            // body: Container(
            //     child: ExpansionTile(
            //         title: Text("9999"),
            //         children: <Widget>[
            //             Container(
            //                 child: Text("mmm"),
            //             )
            //         ],
            //     ),
            // ),
            body: Container(
                    child:Container(
                            child: ListView.builder(
                                itemBuilder: (BuildContext context,int index){
                                    return EntryItem(data[index]);
                                },
                                itemCount: data.length,
                            ),
                        )
                ),
            
        );
    }
}

class Entry{
    Entry(this.title,[this.children = const <Entry>[]]);
    final String title;
    final List<Entry> children;
}

final List<Entry> data = <Entry>[
    Entry(
        'Chapter A',
        <Entry>[
            Entry(
                'Section A0',
                <Entry>[
                    Entry('Item A0.1'),
                    Entry('Item A0.2'),
                    Entry('Item A0.3')
                ]
            ),
            Entry('Section A1'),
            Entry('Section A2')
        ]
    ),
    new Entry('Chapter B',
        <Entry>[
            new Entry('Section B0'),
            new Entry('Section B1'),
        ],
    ),
    new Entry('Chapter C',
        <Entry>[
        new Entry('Section C0'),
        new Entry('Section C1'),
        new Entry('Section C2',
            <Entry>[
            new Entry('Item C2.0'),
            new Entry('Item C2.1'),
            new Entry('Item C2.2'),
            new Entry('Item C2.3'),
            ],
        ),
        ],
    ),
];

class EntryItem extends StatelessWidget{
    EntryItem(this.entry);
    final Entry entry;

    Widget _buildTitles(Entry root){
        if(root.children.isEmpty){
            return ListTile(title: Text(root.title),);
        }
        return ExpansionTile(
            key: PageStorageKey<Entry>(root),
            title: Text(root.title),
            children: root.children.map(_buildTitles).toList(),
        );
    }

    @override
    Widget build(BuildContext context){
        return _buildTitles(entry);
    }
}